/*
 * 文件名：UpLoadCarOutInfoRunble.java	 
 * 时     间：下午7:46:05
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.cloud.threads;

import java.io.IOException;
import java.util.Date;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.cloud.entity.ResultVo;
import com.cz073586.tools.http.HttpManagerUtil;
import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.Tools;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.CarParkOut;
import com.local.model.Log;
import com.local.service.face.LogService;
import com.local.util.HttpClientUtil;

/**
 * 类描述:车辆出场时把出场状态更改
 * 
 * @ClassName: UpLoadCarOutInfoRunble
 * @author Administrator
 * @date 2015年10月10日 下午7:46:05
 */
public class UpDateCarOutInfoRunble implements Runnable
{
	private Log4jUtil log = Log4jUtil.getLog4j(UpDateCarOutInfoRunble.class);
	private CarParkOut carParkOut;
	private LogService logServier;
	
	public UpDateCarOutInfoRunble(com.local.model.CarParkOut carParkOut,LogService logServier)
	{
		super();
		this.carParkOut = carParkOut;
		this.logServier=logServier;
	}
	
	@Override
	public void run()
	{
		// 发送请求
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		try
		{
			
			if (Tools.isEmpty(this.carParkOut))
			{
				return;
			}
			// 获取请求路径
			String uri = CloudConfigManager.instance().getHost()
					+ CloudConfigManager.instance().getCfgValueByKey(
							"sync_car_out_update");
			
			HttpPost post = new HttpPost(uri);
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			String sendData = JsonUtil.getJsonString(this.carParkOut);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity(), "UTF-8");
			
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			
			if(Tools.isEmpty(resultVo))
			{
				System.out.println(this.carParkOut.getCarNo() + "云平台返回空.");
			}
			
			if (resultVo.getResult().equals("success"))
			{
				System.out.println(this.carParkOut.getCarNo() + ",更新出场纪录成功..");
				//this.logServier.deleteCarInfoAndPayBill(this.carParkOut);
			} else
			{
				System.out.println(this.carParkOut.getCarNo() + ",更新出场纪录失败.");
				System.out.println(resultVo.getErrorMsg());
				addErrorLog(this.carParkOut.getUuid(), "更新出场纪录失败.");
			}
			
		} catch (Throwable t)
		{
			this.log.error("", t);
			addErrorLog(this.carParkOut.getUuid(), "更新出场纪录失败.");
			try
			{
      if (httpClient != null)
      {
        httpClient.close();
        }
   }
      catch (IOException localIOException1)
     {
      }
    }
		finally
		{
			try
			{
				if (httpClient != null)
				{
				httpClient.close();
				}
			}
			catch (IOException localIOException2)
			{
				
			}
		}
		
	}
	
	
	public void addErrorLog(String businessUuid,String deScript)
	{
		Log logInfo = new Log();
		logInfo.setCreaterAt(new Date());
		logInfo.setSummary("更新出场信息异常.");
		logInfo.setDescript(deScript);
		logInfo.setLevel("警告");
		logInfo.setSrc("系统");
		logInfo.setStatus(Integer.valueOf(1));
		logInfo.setCls("com.cloud.threads.UpDateCarOutInfoRunble");
        logInfo.setModule(Integer.valueOf(3));
		logInfo.setBusinessUuid(businessUuid);
		this.logServier.addLogInfo(logInfo);

	}
}
